package com.aishua.aishuawallet;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;

import com.aishua.aishuawallet.activity.LoginActivity;
import com.aishua.aishuawallet.activity.WelcomeActivity;
import com.aishua.aishuawallet.bean.Header;
import com.aishua.aishuawallet.utils.CacheUtils;
import com.aishua.aishuawallet.utils.CrashHandler;
import com.aishua.aishuawallet.utils.LogUtils;
import com.appeaser.sublimepickerlibrary.utilities.Config;

import java.util.Locale;

import app.socketlib.com.library.ContentServiceHelper;
import app.socketlib.com.library.socket.SocketConfig;


/**
 * Created by aishua on 2018/8/1.
 */

public class MyAppclication extends Application {

    private static final String TAG = "MyAppclication-";
    @Override
    public void onCreate() {
        super.onCreate();
//       初始化崩溃日志捕捉
        CrashHandler.getInstance().init(this);
//        getLongConnect(getApplicationContext());
        String language = CacheUtils.getlanguage(this, Constant.LAGUAGE);
        LogUtils.v(TAG+"language:"+language);
        Locale locale;
        if (Constant.US_LAGUAGE.equals(language)) {

            locale = Locale.US;

        } else {

            locale = Locale.CHINA;

        }
        changeAppLanguage(locale);

    }

    private String getHeartTCP() {

        Header header = new Header();
        header.setVersion("0311");
        header.setOrderID("0000");
        header.setCommand_ID("1000");
        header.setSenderCode("00000000000000000000");
        String data = header.requestString();
        return data;
    }

    private void getLongConnect(Context mContext) {

        String heart = getHeartTCP();
        SocketConfig socketConfig = new SocketConfig.Builder(mContext)
                .setIp(Constant.SOCKET_IP)//ip
                .setPort(Constant.SOCKET_PORT)//端口
                .setReadBufferSize(10240)//readBuffer
                .setIdleTimeOut(1)//客户端空闲时间,客户端在超过此时间内不向服务器发送数据,则视为idle状态,则进入心跳状态
                .setTimeOutCheckInterval(30)//客户端连接超时时间,超过此时间则视为连接超时
                .setRequestInterval(30)//请求超时间隔时间
                .setHeartbeatRequest(heart)//与服务端约定的发送过去的心跳包
                .setHeartbeatResponse("1001") //与服务端约定的接收到的心跳包
                .builder();
        ContentServiceHelper.bindService(mContext, socketConfig);


    }


    public void changeAppLanguage(Locale locale) {

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        Configuration configuration = getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        getResources().updateConfiguration(configuration, metrics);
//        //重新启动Activity
//        Intent intent = new Intent(this, WelcomeActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        startActivity(intent);

    }

}
